package com.example.a2023110445

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat

import android.widget.Spinner
import com.example.a2023110445.LanguageModel
import com.example.a2023110445.LanguageController

class MainActivity : AppCompatActivity() {
    private var textCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // 区1：动态添加TextView
        val btnAddTextView = findViewById<Button>(R.id.btnAddTextView)
        val linearTextContainer = findViewById<LinearLayout>(R.id.linearTextContainer)

        btnAddTextView.setOnClickListener {
            textCount++
            val textView = TextView(this).apply {
                text = getString(R.string.new_textview_template, textCount)
                textSize = 16f
                setTextColor(ContextCompat.getColor(this@MainActivity, android.R.color.black))
                setPadding(16, 8, 16, 8)
            }
            linearTextContainer.addView(textView)
        }

        // 区2：MVC模式的Program Adviser
        val languageSpinner = findViewById<Spinner>(R.id.languageSpinner)
        val btnConfirm = findViewById<Button>(R.id.btnConfirm)
        val tvResult = findViewById<TextView>(R.id.tvResult)

        // 初始化Spinner数据
        val languages = arrayOf("Kotlin", "Java", "Python")
        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, languages)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        languageSpinner.adapter = adapter

        // 初始化MVC
        val model = LanguageModel()
        val controller = LanguageController(model, languageSpinner, tvResult)

        btnConfirm.setOnClickListener {
            controller.handleConfirm()
        }
    }
}